package menu;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;

public class Menu extends BasicGame {

	Image bg;
	static UnicodeFont font;
	List<MenuOption> menuList;
	MenuOption activeOption;
	Music music;
	Sound choose;
	Sound press;
	
	// input
	Input input;
	boolean upPressed;
	boolean rightPressed;
	boolean downPressed;
	boolean leftPressed;
	boolean confirmPressed;
	boolean cancelPressed;
	
	// key bindings
	int key_up = Input.KEY_UP;
	int key_right = Input.KEY_RIGHT;
	int key_down = Input.KEY_DOWN;
	int key_left = Input.KEY_LEFT;
	int key_confirm = Input.KEY_RETURN;
	int key_cancel = Input.KEY_BACK;
	
	public Menu(String title) {
		super(title);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void init(GameContainer arg0) throws SlickException {
		
		// create font
		String fontPath = "res/Axaxax.ttf";
		font = new UnicodeFont(fontPath , 20, true, false); // Create Instance
		font.addAsciiGlyphs();   // Add Glyphs
		font.addGlyphs(400, 600); // Add Glyphs
		font.getEffects().add(new ColorEffect(java.awt.Color.WHITE)); // Add Effects
		font.loadGlyphs();  // Load Glyphs
		
		// music, sounds & bg
		bg = new Image("res/title2.png"); 
		music = new Music("res/menu_theme.ogg");
		music.loop();
		music.setVolume(.2f);
		choose = new Sound("res/bullet_ricochet_04.ogg");
		press = new Sound("res/underwater_laser.ogg");
		
		// menu options
		menuList = new ArrayList<MenuOption>();
		menuList.add(new MenuOption("New Game"));
		menuList.add(new MenuOption("Difficulty"));
		menuList.add(new MenuOption("Highscores"));
		menuList.add(new MenuOption("Quit"));
		activeOption = menuList.get(0);
	}

	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		Input input = gc.getInput();
		
		if (input.isKeyPressed(key_up)) {
			choose.play(.2f, .3f);
			try {
				activeOption = menuList.get(menuList.indexOf(activeOption) - 1);
			} catch (ArrayIndexOutOfBoundsException e) {
				activeOption = menuList.get(menuList.size() - 1);
			}
		}
		if (input.isKeyPressed(key_down)) {
			choose.play(.2f, .3f);
			try {
				activeOption = menuList.get(menuList.indexOf(activeOption) + 1);
			} catch (IndexOutOfBoundsException e) {
				activeOption = menuList.get(0);
			}
		}
		if (input.isKeyPressed(key_confirm)) {
			press.play(1f, 1f);
			if (activeOption == menuList.get(0)) {
				gc.exit();
			}
			if (activeOption == menuList.get(1)) {
				Settings.setDifficulty(Settings.getDifficulty());
			}
			if (activeOption == menuList.get(3)) {
				System.exit(0);
			}
		}

	}
	
	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		
		// render bg
		bg.draw();
		
		int x = 200;
		int y = 250;
		
		for (MenuOption option : menuList) {
			if (option == activeOption) {
				g.setColor(Color.white);
			} else {
				g.setColor(Color.lightGray);
			}
			g.fillRect(x, y, 400, 50);
			g.setColor(Color.black);
			g.drawRect(x, y, 400, 50);
			font.drawString(x + 20, y + 18, option.getTitle(), Color.darkGray);
			y += 80;
		}
		
		font.drawString(400, 348, Settings.getDifficulty().toString(), Color.darkGray);
		
	}
	
	public static void main(String[] args) throws SlickException {
		AppGameContainer menu = new AppGameContainer(new Menu("Specuspace"));
		menu.setForceExit(false);
		menu.setDisplayMode(800, 600, false);
		menu.start();
//		menu.exit();
	}
	
	
}
